package cn.e2win.biz.user;

import java.sql.SQLException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.stereotype.Repository;

import cn.e2win.base.controller.MyException;
import cn.e2win.base.entity.SysRole;
import cn.e2win.base.entity.SysRoleFunction;
import cn.e2win.base.entity.SysUser;
import cn.e2win.base.entity.SysUserRole;
import cn.e2win.base.json.vo.Pager;
import cn.e2win.biz.base.BaseDAOHibernateImpl;


@Repository
public class UserDaoImpl extends BaseDAOHibernateImpl implements UserDao {
	
	@Override
	public void pagerUser(Pager pager) {
		String sql = "SELECT * FROM ("
				+ " SELECT t0.*, t1.roleNames "
				+ " FROM sys_user t0 LEFT JOIN ("
				+ "  SELECT t0.userId, GROUP_CONCAT(t1.roleName SEPARATOR '＊') AS roleNames"
				+ "  FROM sys_user_role t0, sys_role t1"
				+ "  WHERE t0.roleId = t1.id "
				+ "  GROUP BY t0.userId "
				+ " ) t1 ON t0.id = t1.userId "
				+ ")t WHERE 1=1";
		
		try {
			pager.processSql(sql + Pager.DEF_ORDER_BY);
			processPager(pager);
		} catch (SQLException e) {
			e.printStackTrace();
		}
	}

	@Override
	public Map<String, Object> getUserInfo(Integer id) {
		Map<String, Object> paramMap = new HashMap<String, Object>();
		paramMap.put("id", id);
		String sql = "SELECT t1.coId, t2.coName, t2.address, t2.telNo as coTelNo, t2.faxNo,t2.zipCode, t2.contact, t2.cellPhone, t2.orderFoot,t2.printCustInfoInt,t2.companyType" +
				" ,t1.* FROM sys_user t1 " +
				" LEFT JOIN sys_company  t2 ON  t1.coid = t2.id " +
				" WHERE t1.id=:id";
		return super.get1stRecordBySql(sql, paramMap);
		
	}

	@Override
	public List<SysUser> getUserByAccount(String account) {
		Map<String, Object> paramMap = new HashMap<String, Object>();
		paramMap.put("account", account);
		String hql = "FROM SysUser WHERE 1=1 AND account='"+account+"'";
		return super.getObjectListByHql(hql);
	}
	@Override
	public List<SysUser> getUserByOpenId(String openId) {
		Map<String, Object> paramMap = new HashMap<String, Object>();
		paramMap.put("openId", openId);
		String hql = "FROM SysUser WHERE 1=1 AND openId='"+openId+"'";
		return super.getObjectListByHql(hql);
	}

	@Override
	public SysUser getUserByType(String userType,Integer id)throws MyException  {
		Map<String, Object> paramMap = new HashMap<String, Object>();
		paramMap.put("userType", userType);
		paramMap.put("id", id);
		String sql = "SELECT id FROM sys_user WHERE 1=1 AND userType=:userType AND empId=:id";
		List<Map<String, Object>> list = super.getListBySql(sql, paramMap);
		if(list==null || list.size()!=1){
			throw new MyException("根据员工信息找不到或找到多个用户！");
		}else{
			return (SysUser)getEntity(SysUser.class,(Integer)list.get(0).get("id"));
		}
	}

	@Override
	public Integer tsfCustomerProcessor(Integer oldId,
			Integer newId) {
		Map<String, Object> paramMap = new HashMap<String, Object>();
		paramMap.put("oldId", oldId);
		paramMap.put("newId", newId);
		
		String sql = "UPDATE crm_customer SET processorId=:newId WHERE processorId=:oldId";
		return super.updateBySql(sql, paramMap);
	}

	@Override
	public void pagerLogs(Pager pager) {
		String sql = "SELECT * FROM sys_logs WHERE 1=1";
		
		try {
			pager.processSql(sql);
			processPager(pager);
		} catch (SQLException e) {
			e.printStackTrace();
		}
	}

	@Override
	public List<SysUserRole> getUserRoleListByUserId(Integer userId) {
		return (ArrayList<SysUserRole>)super.getObjectListByHql("FROM SysUserRole WHERE userId=" + userId);
	}


	@Override
	public List<SysRoleFunction> getRoleFunctionListByRoleId(Integer roleId) {
		return (ArrayList<SysRoleFunction>)super.getObjectListByHql("FROM SysRoleFunction WHERE roleId=" + roleId);
	}

	
	@Override
	public List<Map<String, Object>> getUserList(
			Map<String, Object> paramMap) {
		String sql = "SELECT * FROM (" +
				" SELECT id, userName AS name " +
				" FROM sys_user " +
				" WHERE activeStatusInt=1)AS t" +
				" WHERE 1=1 " +
				" ORDER BY name DESC";

		return super.getListBySqlAutoParam(sql, paramMap);
	}

	@Override
	public Integer delUserRoleByUserId(Integer userId) {
		String sql = "DELETE FROM sys_user_role WHERE userId =" + userId;
		return super.updateBySql(sql, null);
	}
}
